package com.yonyou.pmclouds.constructionlog.process;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO;
import com.yonyou.pmclouds.procedure.entity.ProcedureVO;
import com.yonyou.pmclouds.procedure.rmiitf.ProcedureQuery;
import com.yonyou.pmclouds.projectlog.entity.ConstructionContentVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogDetailVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogJobVo;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogMaintain;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Calendar;

/**
 * 智能日志记录维护后置处理器，新建或更新都使用
 * 出错不要影响其他本来的流程
 * @author xiejianyu
 * 2019-09-18
 */
@Slf4j
@Component
public class ConstructionLogProjectLogPostProcess implements SingleProcessor<ConstructionLogVO> {

    private static ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private ProcedureQuery procedureQuery;
    @Autowired
    private ProjectLogMaintain projectLogMaintain;

    @Override
    public void process(ConstructionLogVO vo, ConstructionLogVO orig) throws BusinessException {
        //如果使用标准的 SingleInsert SingleUpdate SingleDelete 模板进行操作的话，新建时orig 为null，更新或删除时orig不为null
        //目前只有inspectarea部分在更新的范围内
        try {
            ProjectLogJobVo jobVo = new ProjectLogJobVo();//最终放入队列的任务对象
            String procedureName = "施工记录";
            if (StringUtils.isNotEmpty(vo.getPk_process())) {
                ProcedureVO procedureVO = procedureQuery.getById(vo.getPk_process());
                procedureName = procedureVO.getSname();
            }
            if (orig==null) {//新增记录
                String summary = MessageFormat.format(ConstructionContentVo.SUMMARY_TEMPLATE, vo.getInspectarea(), procedureName);
                ConstructionContentVo logDetailContent = new ConstructionContentVo();
                logDetailContent.setPkBill(vo.getPk_constructionlog());
                logDetailContent.setBillType(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE);
                logDetailContent.setSummary(summary);

                ProjectLogDetailVo logDetail = new ProjectLogDetailVo();
                logDetail.setDetailType(ProjectLogDetailVo.CONSTRUCTION_CONTENT);
                logDetail.setPkContentSource(vo.getPk_constructionlog());
                logDetail.setContentJson(objectMapper.writeValueAsString(logDetailContent));
                logDetail.setCreator(vo.getCreator());
                logDetail.setCreationtime(vo.getCreationtime());
                logDetail.setModifier(vo.getCreator());
                logDetail.setModifytime(vo.getCreationtime());

                jobVo.setPkTenant(vo.getPk_tenant());
                jobVo.setPkProject(vo.getPk_project());

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(vo.getCreationtime());
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                jobVo.setLogDate(calendar.getTime());
                jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_DETAIL);
                jobVo.setEventTime(vo.getCreationtime());
                jobVo.setUserId(vo.getCreator());
                jobVo.setAttachment(logDetail);
            } else {//更新记录
                String summary = MessageFormat.format(ConstructionContentVo.SUMMARY_TEMPLATE, vo.getInspectarea(), procedureName);
                ConstructionContentVo logDetailContent = new ConstructionContentVo();
                logDetailContent.setPkBill(orig.getPk_constructionlog());
                logDetailContent.setBillType(BillTypeConst.CONSTRUCTIONLOG_BILLTYPE);
                logDetailContent.setSummary(summary);

                ProjectLogDetailVo logDetail = new ProjectLogDetailVo();
                logDetail.setDetailType(ProjectLogDetailVo.CONSTRUCTION_CONTENT);
                logDetail.setPkContentSource(orig.getPk_constructionlog());
                logDetail.setContentJson(objectMapper.writeValueAsString(logDetailContent));
                logDetail.setCreator(vo.getModifier());
                logDetail.setCreationtime(vo.getModifytime());
                logDetail.setModifier(vo.getModifier());
                logDetail.setModifytime(vo.getModifytime());

                jobVo.setPkTenant(orig.getPk_tenant());
                jobVo.setPkProject(orig.getPk_project());

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(orig.getCreationtime());
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                jobVo.setLogDate(calendar.getTime());
                jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_DETAIL);
                jobVo.setEventTime(vo.getModifytime());
                jobVo.setUserId(vo.getModifier());
                jobVo.setAttachment(logDetail);
            }
            //pmCloudCacheManager.addObjToBlockingQueue(ProjectLogJobVo.JOB_QUEUE_NAME, jobVo);
            projectLogMaintain.upsert(jobVo);
        } catch (Exception e) {
            log.error("施工记录--智能日志处理出错，记录pk：{}", vo.getPk_constructionlog());
            log.error("异常信息", e);
        }
    }
}
